package com.clippick.walkthrough;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.TextView;

import co.kr.clippick.R;

public class HtmlPageAdapter extends PagerAdapter {

	private Context mContext;
	private LayoutInflater inflater;
	private List<String> pages = new ArrayList<String>();

	public HtmlPageAdapter(Context context, List<String> pageList) {
		// TODO Auto-generated constructor stub
		mContext = context;
		pages.removeAll(this.pages);
		pages.addAll(pageList);
		inflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return pages.size();
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		// TODO Auto-generated method stub
		return arg0 == arg1;
	}

	@Override
	public Object instantiateItem(View pager, int position) {
		View v = null;
		v = inflater.inflate(R.layout.html_view, null);

		TextView t = (TextView) v.findViewById(R.id.text);
		String text = String.valueOf(position + 1) + "/"
				+ String.valueOf(pages.size());
		t.setText(text);

		WebView web = (WebView) v.findViewById(R.id.web);
		web.setVerticalScrollBarEnabled(false);
		
		WebSettings settings = web.getSettings();
		settings.setUseWideViewPort(true);
		settings.setLoadWithOverviewMode(true);
		settings.setJavaScriptEnabled(true);

		web.loadUrl(pages.get(position));

		((ViewPager) pager).addView(v, 0);

		return v;
	}

	@Override
	public void destroyItem(View pager, int position, Object view) {
		((ViewPager) pager).removeView((View) view);
	}

	@Override
	public void restoreState(Parcelable arg0, ClassLoader arg1) {
		
	}

	@Override
	public Parcelable saveState() {
		return null;
	}

	@Override
	public void startUpdate(View arg0) {
	}

	@Override
	public void finishUpdate(View arg0) {
	}
}
